using Headspring.Training.Domain.Model;
using Headspring.Training.Domain.Services;
using Headspring.Training.WebUI.Views;

namespace Headspring.Training.WebUI.Presenters
{
	public class SearchPresenter
	{
		private readonly IEmployeeRepository _employeeRepository;
		private readonly IWorkOrderRepository _workOrderRepository;
		private IWorkflowCoordinator _coordinator;

		public SearchPresenter(IEmployeeRepository employeeRepository, IWorkOrderRepository workOrderRepository,
		                       IWorkflowCoordinator coordinator)
		{
			_employeeRepository = employeeRepository;
			_workOrderRepository = workOrderRepository;
			_coordinator = coordinator;
		}

		public void Initialize(ISearchWorkOrderView view)
		{
			view.SetCreators(_employeeRepository.GetAllEmployees());
			view.SetAssignees(_employeeRepository.GetAllEmployees());
			view.SetStatuses(WorkOrderStatus.GetAllItems());
		}

		public void PerformSearch(ISearchWorkOrderView view)
		{
			Employee creator = view.GetSelectedCreator();
			Employee assignee = view.GetSelectedAssignee();
			WorkOrderStatus status = view.GetSelectedStatus();

			WorkOrderSearchSpecification specification = new WorkOrderSearchSpecification();
			specification.MatchCreator(creator);
			specification.MatchAssignee(assignee);
			specification.MatchStatus(status);
			WorkOrder[] orders = _workOrderRepository.GetWorkOrders(specification);
			view.SetMatchingWorkOrders(orders);
		}

		public void RequestSearch(ISearchWorkOrderView view)
		{
			_coordinator.GoToWorkOrderSearch(view.GetSelectedCreator(), view.GetSelectedAssignee(), view.GetSelectedStatus());
		}
	}
}